import java.awt.Container;
import java.util.Iterator;
import java.util.Set;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import listener.TextMouseListener;
import ui.CloseListener;
import dispatcher.DataDispatcher;


public class UIcreator {
	
	private	JFrame jFrame;
	private	Box filesBox;
	private DataDispatcher dispatcher;
	
	public UIcreator(DataDispatcher dispatcher) {
		this.jFrame = new JFrame();
		this.filesBox = new Box(BoxLayout.Y_AXIS);
		this.dispatcher = dispatcher;
	}
	
	public void createUI() {
		// create ui
		Container container = jFrame.getContentPane();
		JButton jButton = new JButton("close");
		jButton.addActionListener(new CloseListener(this.jFrame));
		container.add(jButton);
	}
	
	public void setFiles(Set<String> filenames) {
		this.filesBox.removeAll();
		
		Iterator<String> it = filenames.iterator();
		while (it.hasNext()) {
			String name = it.next();
			JButton button = new JButton(name);
			button.addMouseListener(new TextMouseListener(name, this.dispatcher));
			this.filesBox.add(button);
		}
		
		if (filesBox.getParent() == null) {
			this.jFrame.add(this.filesBox);
			this.jFrame.setVisible(true);
		}
	}
}
